package com.franklin.ideaplugin.easytesting.configuration;

import com.franklin.ideaplugin.easytesting.configuration.ui.UiConfigForm;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;

/**
 * @author Ye Junhui
 * @since 2023/8/10
 */
public class EasyTestingUIConfigurable implements Configurable {

    private UiConfigForm uiConfigForm;

    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        return "UI Setting";
    }

    @Override
    public @Nullable JComponent createComponent() {
        UIConfigPersistentState uiConfigPersistentState = UIConfigPersistentState.getInstance();
        uiConfigForm = new UiConfigForm(uiConfigPersistentState.headerHeight, uiConfigPersistentState.historyWidth);
        return uiConfigForm.getMainPanel();
    }

    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public void apply() throws ConfigurationException {
        if (Objects.nonNull(uiConfigForm)) {
            UIConfigPersistentState uiConfigPersistentState = UIConfigPersistentState.getInstance();
            uiConfigPersistentState.headerHeight = uiConfigForm.getHeaderHeight();
            uiConfigPersistentState.historyWidth = uiConfigForm.getHistoryWidth();
        }
    }
}
